package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.course.bean.BookBackBean;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.StringUtil;
import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class PeiXunBookShelfRvAdapter extends RecyclerView.Adapter<PeiXunBookShelfRvAdapter.BookShelfViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<BookBackBean.ResultListBean> dataList;

    public PeiXunBookShelfRvAdapter(Context context,ArrayList<BookBackBean.ResultListBean> dataList) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public BookShelfViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.peixun_bookshelf_rv_item, viewGroup, false);
        return new BookShelfViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookShelfViewHolder viewHolder, int position) {
        BookBackBean.ResultListBean resultListBean = dataList.get(position);
        BookBackBean.ResultListBean.DataBean data = resultListBean.getData();
        viewHolder.tv_course_name.setText(data.getName());
        viewHolder.layout_book.setOnClickListener(new MyOnClickItemListener(data,position));
        double price = data.getPrice();
        if (2 == data.getType()) {
            //线下课程
            if (price != 0.0) {
                viewHolder.tv_course_price.setText("¥" + price);
            } else {
                viewHolder.tv_course_price.setText("");
            }
            if (!StringUtil.isEmpty(data.getStartTime())&&data.getStartTime().length()>10){
                viewHolder.tv_course_total_time.setText("开课时间：" + data.getStartTime().substring(0, 10));//
            }else {
                viewHolder.tv_course_total_time.setText("开课时间：");//
            }
            viewHolder.tv_course_time.setText("地点：" + data.getCity());//
            GlideUtils.loaderHanldeRoundImage(context, data.getCover(), viewHolder.img_course_logo,R.mipmap.offline_default, DensityUtils.dp2px(context,8));

        } else {

            double secKillPrice = data.getSecKillPrice();
            if (secKillPrice != 0.0) {
                viewHolder.tv_course_price.setText("¥" + secKillPrice);
            } else {
                if (price != 0.0) {
                    viewHolder.tv_course_price.setText("¥" + price);
                } else {
                    viewHolder.tv_course_price.setText("");
                }
            }
            if (!StringUtil.isEmpty(resultListBean.getCoursesHourCount())) {
                viewHolder.tv_course_total_time.setText("课时：" + resultListBean.getCoursesHourCount()+"课时");
            }else {
                viewHolder.tv_course_total_time.setText("课时：");
            }

            if (!StringUtil.isEmpty(resultListBean.getTotleTime())) {
                viewHolder.tv_course_time.setText("时长：" + resultListBean.getTotleTime());
            }else {
                viewHolder.tv_course_time.setText("时长：");
            }
            Glide.with(context).load(data.getCover()).placeholder(R.mipmap.online_default).into(viewHolder.img_course_logo);
            GlideUtils.loaderHanldeRoundImage(context, data.getCover(), viewHolder.img_course_logo,R.mipmap.online_default, DensityUtils.dp2px(context,8));

        }

        viewHolder.btnDelete.setOnClickListener(new MyOnClickListener(data,position));
    }

    class MyOnClickListener implements View.OnClickListener{

        BookBackBean.ResultListBean.DataBean data;
        int position;

        public MyOnClickListener(BookBackBean.ResultListBean.DataBean data, int position) {
            this.data = data;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (deleteBook != null) {
                deleteBook.onDelete(data, position);
            }
        }
    }

    class MyOnClickItemListener implements View.OnClickListener{

        BookBackBean.ResultListBean.DataBean data;
        int position;

        public MyOnClickItemListener(BookBackBean.ResultListBean.DataBean data, int position) {
            this.data = data;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (2!=data.getAuditStatus()){
                ToastTools.showPublicToast("该商品已下架");
                return;
            }
            StringBuffer sb = new StringBuffer();
            int type = data.getType();
            sb.append(type);
            long id = data.getId();
            sb.append(id);
            String substring = null;
            if (!StringUtil.isEmpty(data.getCover())) {
               substring =  data.getCover().substring(49, data.getCover().length());
            }
            context.startActivity(CourseDetailActivity.getIntent(context, sb.toString(), substring,""));
        }
    }
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class BookShelfViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_course_logo)
        ImageView img_course_logo;
        @BindView(R.id.tv_course_name)
        TextView tv_course_name;
        @BindView(R.id.tv_course_total_time)
        TextView tv_course_total_time;
        @BindView(R.id.tv_course_time)
        TextView tv_course_time;
        @BindView(R.id.tv_course_price)
        TextView tv_course_price;
        @BindView(R.id.layout_book)
        LinearLayout layout_book;
        @BindView(R.id.btnDelete)
        Button btnDelete;

        public BookShelfViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private DeleteBook deleteBook;

    public void setDeleteBook(DeleteBook deleteBook) {
        this.deleteBook = deleteBook;
    }

    public interface DeleteBook {
        void onDelete(BookBackBean.ResultListBean.DataBean bean, int position);
    }

}
